/**
 * Created by gongxun on 17/1/13.
 */

var PdkCard = cc.Sprite.extend({
    mCardNum : null,
    mKind : null,
    mSeat : null,

    //手动拼ui
    mSpriteNum : null,
    mSpriteTagBig : null,
    mSpriteTagSmall : null,

    //染色
    mSpritemMask : null,

    //是否抬起
    mIsUp : null,
    //是否染色
    mIsColor : null,

    ctor : function(){
        this._super();

        this.mIsUp = false;
        this.mIsColor = false;
        this.setAnchorPoint(cc.p(0.5, 0.5));

        this.mSpriteNum = new cc.Sprite();
        this.mSpriteTagBig = new cc.Sprite();
        this.mSpriteTagSmall = new cc.Sprite();

        this.addChild(this.mSpriteNum);
        this.addChild(this.mSpriteTagBig);
        this.addChild(this.mSpriteTagSmall);

        this.mSpriteNum.setAnchorPoint(cc.p(0.5, 1));
        this.mSpriteNum.setPosition(cc.p(35, 190));

        this.mSpriteTagBig.setPosition(cc.p(100, 60));

        this.mSpriteTagSmall.setScale(0.5);
        this.mSpriteTagSmall.setPosition(cc.p(35, 110));

        //染色
        this.mSpritemMask = new cc.Scale9Sprite("b_poker_mask_scale9.png");
        this.mSpritemMask.setVisible(false);
        this.addChild(this.mSpritemMask);
    },

    setCardNum : function(num){
        if (mo.pdkCardHelper.getExistCardNum(num)){
            this.mCardNum = num;
        }else{
            this.mCardNum = 0;
        }

        this.loadCardTexture();
    },

    //kind 0:inhand 1:deal
    setCardKind : function(seat, kind){
        this.mSeat = seat;
        this.mKind = kind;

        if (seat == 0){
            if (kind == 0){
                this.setScale(mo.pdkCardHelper.mInhandCardScale);
            }else if (kind == 1){
                this.setScale(mo.pdkCardHelper.mDealCardScale);
            }
        }else{
            if (kind == 0){
                this.setScale(mo.pdkCardHelper.mInhandCardScale);
            }else if (kind == 1){
                this.setScale(mo.pdkCardHelper.mDealCardScale);
            }
        }
    },

    loadCardTexture : function(){
        if (this.mCardNum == 0){
            this.setSpriteFrame("card_bg0.png");


            this.mSpriteNum.setVisible(false);
            this.mSpriteTagBig.setVisible(false);
            this.mSpriteTagSmall.setVisible(false);

        }else{
            this.setSpriteFrame("b_bg.png");

            this.mSpriteNum.setVisible(true);
            this.mSpriteTagBig.setVisible(true);
            this.mSpriteTagSmall.setVisible(true);

            //遮罩
            this.mSpritemMask.setPosition(cc.p(this.getContentSize().width / 2, this.getContentSize().height / 2));
            this.mSpritemMask.setContentSize(cc.size(141, 197));

            var num = this.mCardNum;
            var suit = this.getCardSuit(num);
            var tempNum = num - suit * 13 - 1;

            var suitImages = ["b_bigtag_3.png", "b_bigtag_2.png", "b_bigtag_1.png", "b_bigtag_0.png"];
            var tagBigStr = suitImages[suit];
            this.mSpriteTagBig.setSpriteFrame(tagBigStr);
            this.mSpriteTagSmall.setSpriteFrame(tagBigStr);

            //card
            var useRed = [false, true, false, true];
            var numImageStr = useRed[suit] ? "b_red_" + tempNum + ".png" : "b_black_" + tempNum + ".png";
            this.mSpriteNum.setSpriteFrame(numImageStr);
        }
    },

    getCarcJisuanNum : function(){
        var num = this.mCardNum - 1;
        var temp = num % 13;
        temp += 1;
        if (temp > 10){
            temp = 10;
        }

        //王
        if (this.mCardNum >= 53){
            temp = 10;
        }

        return temp;
    },

    setPosition : function(point){
        if (this.mSeat == 0 && this.mKind == 0){
            var y = mo.pdkCardHelper.getInhandCardHeight();
            point.y = this.mIsUp ? (y + 20) : y;
        }
        this._super(point);
    },

    //返回牌花色 0:tao 1:xin 2:mei 3:fang
    getCardSuit : function(num){
        var temp = num - 1;
        return parseInt(temp / 13);
    },

    //设置抬起状态
    setIsUpStats : function (b) {
        this.mIsUp = b;
        var y = mo.pdkCardHelper.getInhandCardHeight();
        this.setPositionY(this.mIsUp ? (y + 20) : y);
    },

    //设置选中状态
    setIsColorStats : function(b){
        this.mIsColor = b;
        this.mSpritemMask.setVisible(b);
    },

    //透明度
    setOpacity : function(opacity){
        // cc.log("opacity:" + opacity);

        cc.Node.prototype.setOpacity.call(this, opacity);
        // this._super(opacity);

        this.mSpriteNum.setOpacity(opacity);
        this.mSpriteTagBig.setOpacity(opacity);
        this.mSpriteTagSmall.setOpacity(opacity);
    }
});